package org.deng.learn.r2dbc.mariadb;

import org.mariadb.r2dbc.MariadbConnectionConfiguration;
import org.mariadb.r2dbc.MariadbConnectionFactory;
import org.mariadb.r2dbc.api.MariadbConnection;
import org.mariadb.r2dbc.api.MariadbResult;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * @author 邓力宾
 * @date 2021/2/23 16:59
 * @desc
 */
public class Main {
    public static void main(String[] args) {
        MariadbConnectionConfiguration conf = MariadbConnectionConfiguration.builder()
                .host("192.168.2.177")
                .port(3306)
                .username("root")
                .password("denglibin")
                .database("serv_plat_sys")
                .build();

        MariadbConnectionFactory factory = new MariadbConnectionFactory(conf);

        MariadbConnection connection = factory.create().block();
        Flux<String> flux = connection.createStatement("SELECT id,username, full_name,is_valid FROM sys_user")
                .execute()
                .flatMap(r -> r.map((row, metadata) -> {
                    StringBuilder sb = new StringBuilder();
                    metadata.getColumnNames().forEach(column->{
                        sb.append(column+":"+row.get(column, Object.class));
                        sb.append("\n");
                    });
                    return sb.toString();
                }));

//        String s = flux.blockLast();
//        System.out.println(s);
        flux.subscribe(s-> System.out.println(s));
        LockSupport.park();

    }

}
